package com.bRoom.service.impl;

import com.bRoom.common.Conts;
import com.bRoom.common.ServerResponse;
import com.bRoom.dao.CategoryMapper;
import com.bRoom.pojo.Category;
import com.bRoom.service.ICategoryService;
import com.bRoom.util.DateTimeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Service("iCategoryService")
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public ServerResponse<Category> addCategory(Category category){
           if (category!=null){
                 category.setCategoryStatus(Conts.CATEGORY_TYPE.USERING.getCode());
                int insertCount=categoryMapper.insert(category);
                if (insertCount>0){
                     return ServerResponse.createBySuccessMsg("添加成功");
                }
                return ServerResponse.createByErrorMsg("添加失败");
           }
           return ServerResponse.createByErrorMsg("参数错误");
    }


    public  ServerResponse<Category> updateCategory(Category category){

             Category updateCategory=new Category();
             updateCategory.setCategoryId(category.getCategoryId());
             updateCategory.setCategoryName(category.getCategoryName());
             updateCategory.setCategoryStatus(category.getCategoryStatus());
             updateCategory.setParentId(category.getParentId());
             String updateTime= DateTimeUtil.DatetoStr(new Date());
             updateCategory.setUpdatetime(DateTimeUtil.StringToDate(updateTime));
             int updateCount=categoryMapper.updateByPrimaryKeySelective(updateCategory);
             if (updateCount>0){
                  return ServerResponse.createBySuccessMsg("更新成功");
             }
             return ServerResponse.createByErrorMsg("更新失败");
    }

    public ServerResponse<PageInfo> categoryList(int pageSize, int pageNum){
            PageHelper.startPage(pageSize,pageNum);
           List<Category> categoryList=categoryMapper.selectList();
           if (CollectionUtils.isEmpty(categoryList)){
                return ServerResponse.createByErrorMsg("查询失败");
           }
           PageInfo result=new PageInfo(categoryList);
           result.setList(categoryList);
           return ServerResponse.createBySuccessData(result);
    }

    public  ServerResponse categoryDetail(Integer categoryId){
           Category category=categoryMapper.categoryDetail(categoryId);
           if (category!=null){
                return ServerResponse.createBySuccessData(category);
           }
           return ServerResponse.createByErrorMsg("信息查找错误");
    }

    public ServerResponse<List<Category>>  fingCategoryId(Integer parentId){
         List<Category> categoryList=categoryMapper.findParentId(parentId);
         if (CollectionUtils.isEmpty(categoryList)){
              return ServerResponse.createByErrorMsg("查找信息失败");
         }
         return ServerResponse.createBySuccessData(categoryList);
    }

    public ServerResponse finddeepChildrenCategory(Integer categoryId){
                     Set<Category> categorySet= Sets.newHashSet();
                     findChildrenCategory(categorySet,categoryId);
                     List<Integer> categoryIdList=Lists.newArrayList();
                     if (categoryId!=null){
                           for (Category  categoryItem :categorySet){
                                      categoryIdList.add(categoryItem.getCategoryId());
                           }
                     }
                     return ServerResponse.createBySuccessData(categoryIdList);
    }

    private Set<Category>  findChildrenCategory(Set<Category> categorySet,Integer categoryId){
          Category category=categoryMapper.categoryDetail(categoryId);
               if (category!=null){
                    categorySet.add(category);
               }

               List<Category> categoryList=categoryMapper.findParentId(categoryId);
               for (Category categoryItem :categoryList){
                     findChildrenCategory(categorySet,categoryItem.getCategoryId());
               }
               return categorySet;
        }

}
